package com.example.personalmemo;

import com.example.personalmemo.recordInfo.MyItem;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.FileDescriptor;

public class DataAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    private RdbStore store;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        HiLog.info(LABEL_LOG, "DataAbility onStart");

        initData();
    }

    // 数据库初始化  建立数据库 和 建表
    private void initData() {
        HiLog.info(LABEL_LOG, "DataAbility initData");

        DatabaseHelper dh = new DatabaseHelper(getContext());
        StoreConfig config = StoreConfig.newDefaultConfig("myaccdata.db");
        RdbOpenCallback callback = new RdbOpenCallback() {
            @Override
            public void onCreate(RdbStore rdbStore) {
//          创建表
                rdbStore.executeSql("create table if not exists infodata(id integer primary key autoincrement,title text,textinfo text,time text)");
            }

            @Override
            public void onUpgrade(RdbStore rdbStore, int i, int i1) {

            }
        };
        store = dh.getRdbStore(config, 1, callback, null);
    }

    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        String table = uri.getDecodedPathList().get(1);
        String action = uri.getDecodedPathList().get(2);
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, table);


        if (table.equals("user")) {
            switch (action) {
                case "login":
                    return store.query(rdbPredicates, columns);
            }
        } else if ("infodata".equals(table)) {
            switch (action) {
                case "queryList":
                    return store.query(rdbPredicates, columns);
            }
        }

        return null;
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {
        HiLog.info(LABEL_LOG, "DataAbility insert");
        String table = uri.getDecodedPathList().get(1);
        String action = uri.getDecodedPathList().get(2);

        long insert = -1;
        if (table.equals("infodata")) {
            switch (action) {
                case "sava":
                    insert = store.insert(table, value);
                    DataAbilityHelper.creator(this).notifyChange(uri);
            }

        }

        return (int) insert;
    }


    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        String table = uri.getDecodedPathList().get(1);
        String action = uri.getDecodedPathList().get(2);
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, table);
        int delete = 0;
        if ("infodata".equals(table)) {
            switch (action) {
                case "delList":
                    delete = store.delete(rdbPredicates);
                    DataAbilityHelper.creator(this).notifyChange(uri);
            }
        }
        return delete;
    }

    /**
     * 更新数据库
     *
     * @param uri        地址
     * @param value      更新内容
     * @param predicates 更新索引
     * @return
     */
    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {
        String table = uri.getDecodedPathList().get(1);
        String action = uri.getDecodedPathList().get(2);
        RdbPredicates rdbPredicates = DataAbilityUtils.createRdbPredicates(predicates, table);
        int update = -1;
        if (table.equals("infodata")) {
            switch (action) {
                case "update":
                    update = store.update(value, rdbPredicates);
                    HiLog.info(LABEL_LOG,String.valueOf(update));

                    DataAbilityHelper.creator(this,uri).notifyChange(uri);
            }
        }
        return update;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        return null;
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}